package laf.socf.simulator.sensors;

import laf.socf.simulator.SensorServer;
import laf.socf.simulator.gui.Robot;
import org.freedesktop.dbus.DBusInterface;

/**
 *
 * @author laf
 */
public class Sonar implements Sensor, DBusInterface {

    public static final int TYPE_FRONT = 0;
    public static final int TYPE_LEFT = 1;
    public static final int TYPE_RIGHT = 2;

    private int type;
    private Robot robot;
    private int count;

    public Sonar() throws Exception {

    }

    public Sonar(Robot robot, int type, int count) throws Exception {
        this.robot = robot;
        this.type = type;
        this.count = count;
        init();
    }

    public void init() throws Exception {
        String path = "";
        switch (type) {
            case TYPE_FRONT:
                path = "/server/SonarFront";
                break;
            case TYPE_LEFT:
                path = "/server/SonarLeft";
                break;
            case TYPE_RIGHT:
                path = "/server/SonarRight";
                break;
        }
        SensorServer.getInstance().exportObject(path + this.count,
               this);
    }

    public boolean isRemote() {
        return true;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getSensorName() {
        String ret = "Sonar ";
        if (this.type == TYPE_FRONT) {
            ret += "Front";
        } else if (this.type == TYPE_LEFT) {
            ret += "Left";
        } else if (this.type == TYPE_RIGHT) {
            ret += "Right";
        }
        return ret;
    }

    public void startSensor() {

    }

    public void stopSensor() {

    }

    public int getDistance() {
        if (this.type == TYPE_FRONT) {
            return this.robot.getFrontObstacleDistance();
        } else if (this.type == TYPE_RIGHT) {
            return this.robot.getRightObstacleDistance();
        } else {
            return 0;
        }
    }

}
